﻿using AutoMapper;
using B.R.RabbitRoot.Domain;
using B.R.RabbitRoot.Domain.animal_husbandry;
using B.R.RabbitRoot.ErrorCode;
using B.R.RabbitRoot.Infrastructure.Interface;
using B.R.RabbitRoot.Read.API.Applications.Commands.FarmInfoCommands;
using B.R.RabbitRoot.Read.API.Dtos;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace B.R.RabbitRoot.Read.API.Applications.CommandHandlers.FarmInfoCommandHandles
{
    /// <summary>
    /// 企业信息查询处理
    /// </summary>
    public class FarmInfoQueryCommandHandler : IRequestHandler<FarmInfoQueryCommand, APIResult<FarmInfoDTO>>
    {
        private readonly IBaserepository<FarmInfo> _farmInfo;
        private readonly IMapper mapperl;
        public FarmInfoQueryCommandHandler(IBaserepository<FarmInfo> farmInfo, IMapper mapperl)
        {
            _farmInfo = farmInfo;
            this.mapperl = mapperl;
        }

        public async Task<APIResult<FarmInfoDTO>> Handle(FarmInfoQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<FarmInfoDTO> result = new APIResult<FarmInfoDTO>();
           var list =await _farmInfo.GetAll().Where(m => m.SourceInfoId == request.SourceInfoId).OrderBy(m=>m.enterpriseID).LastOrDefaultAsync();
         
           var obj= mapperl.Map<FarmInfoDTO>(list);
            return await Task.FromResult(result.Success(obj));
        }
    }
}
